package com.ruoyi.zsyh.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.zsyh.domain.ZsyhStoreOrderRel;
import com.ruoyi.zsyh.mapper.ZsyhStoreOrderMapper;
import com.ruoyi.zsyh.domain.ZsyhStoreOrder;
import com.ruoyi.zsyh.service.IZsyhStoreOrderService;
import com.ruoyi.common.core.text.Convert;

/**
 * 店铺预订单Service业务层处理
 * 
 * @author yuexl
 * @date 2022-03-02
 */
@Service
public class ZsyhStoreOrderServiceImpl implements IZsyhStoreOrderService 
{
    @Autowired
    private ZsyhStoreOrderMapper zsyhStoreOrderMapper;

    /**
     * 查询店铺预订单
     * 
     * @param id 店铺预订单ID
     * @return 店铺预订单
     */
    @Override
    public ZsyhStoreOrder selectZsyhStoreOrderById(String id)
    {
        return zsyhStoreOrderMapper.selectZsyhStoreOrderById(id);
    }

    /**
     * 查询店铺预订单列表
     * 
     * @param zsyhStoreOrder 店铺预订单
     * @return 店铺预订单
     */
    @Override
    public List<ZsyhStoreOrder> selectZsyhStoreOrderList(ZsyhStoreOrder zsyhStoreOrder)
    {
        return zsyhStoreOrderMapper.selectZsyhStoreOrderList(zsyhStoreOrder);
    }

    /**
     * 新增店铺预订单
     * 
     * @param zsyhStoreOrder 店铺预订单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertZsyhStoreOrder(ZsyhStoreOrder zsyhStoreOrder)
    {
        zsyhStoreOrder.setCreateTime(DateUtils.getNowDate());
        int rows = zsyhStoreOrderMapper.insertZsyhStoreOrder(zsyhStoreOrder);
        insertZsyhStoreOrderRel(zsyhStoreOrder);
        return rows;
    }

    /**
     * 修改店铺预订单
     * 
     * @param zsyhStoreOrder 店铺预订单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateZsyhStoreOrder(ZsyhStoreOrder zsyhStoreOrder)
    {
        zsyhStoreOrder.setUpdateTime(DateUtils.getNowDate());
        zsyhStoreOrderMapper.deleteZsyhStoreOrderRelByOrderId(zsyhStoreOrder.getId());
        insertZsyhStoreOrderRel(zsyhStoreOrder);
        return zsyhStoreOrderMapper.updateZsyhStoreOrder(zsyhStoreOrder);
    }

    /**
     * 新增店铺预订单
     *
     * @param zsyhStoreOrder 店铺预订单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertZsyhStoreOrderApplet(ZsyhStoreOrder zsyhStoreOrder)
    {
        int rows = zsyhStoreOrderMapper.insertZsyhStoreOrder(zsyhStoreOrder);
        return rows;
    }

    /**
     * 修改店铺预订单
     *
     * @param zsyhStoreOrder 店铺预订单
     * @return 结果
     */
    @Transactional
    @Override
    public int updateZsyhStoreOrderApplet(ZsyhStoreOrder zsyhStoreOrder)
    {
        return zsyhStoreOrderMapper.updateZsyhStoreOrder(zsyhStoreOrder);
    }

    /**
     * 删除店铺预订单对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteZsyhStoreOrderByIds(String ids)
    {
        zsyhStoreOrderMapper.deleteZsyhStoreOrderRelByOrderIds(Convert.toStrArray(ids));
        return zsyhStoreOrderMapper.deleteZsyhStoreOrderByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除店铺预订单信息
     * 
     * @param id 店铺预订单ID
     * @return 结果
     */
    @Override
    public int deleteZsyhStoreOrderById(String id)
    {
        zsyhStoreOrderMapper.deleteZsyhStoreOrderRelByOrderId(id);
        return zsyhStoreOrderMapper.deleteZsyhStoreOrderById(id);
    }

    /**
     * 新增大区会长预订单详细信息
     * 
     * @param zsyhStoreOrder 店铺预订单对象
     */
    public void insertZsyhStoreOrderRel(ZsyhStoreOrder zsyhStoreOrder)
    {
        List<ZsyhStoreOrderRel> zsyhStoreOrderRelList = zsyhStoreOrder.getZsyhStoreOrderRelList();
        String id = zsyhStoreOrder.getId();
        if (StringUtils.isNotNull(zsyhStoreOrderRelList))
        {
            List<ZsyhStoreOrderRel> list = new ArrayList<ZsyhStoreOrderRel>();
            for (ZsyhStoreOrderRel zsyhStoreOrderRel : zsyhStoreOrderRelList)
            {
                zsyhStoreOrderRel.setOrderId(id);
                list.add(zsyhStoreOrderRel);
            }
            if (list.size() > 0)
            {
                zsyhStoreOrderMapper.batchZsyhStoreOrderRel(list);
            }
        }
    }
}
